// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const axios = require('axios')

module.exports = function (api) {
  api.loadSource(({
    addCollection
  }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const posts = addCollection('Post')
    const tags = addCollection('Tag')

    // makes all ids in the `tags` field reference a `Tag`
    posts.addReference('tags', 'Tag')

    tags.addNode({
      id: '1',
      title: 'The author'
    })
   

    posts.addNode({
      id: '1',
      title: 'A post',
      tags: ['1']
    })
    tags.addNode({
      id: '3',
      title: 'The author 3'
    })
    posts.addNode({
      id: '2',
      title: 'A post 2',
      tags: ['3']
    })
    posts.addNode({
      id: '3',
      title: 'A post 3',
      tags: ['3']
    })
  })

  // api.loadSource(async ({addCollection}) => {
  //   const collection = addCollection('Post')

  //   const { data } = await axios.get('http://jsonplaceholder.typicode.com/posts')

  //   for (const item of data) {
  //     collection.addNode({
  //      ...item
  //     })
  //   }
  // })
}